package com.web.controller.web;

import com.web.dao.MemberMapper;
import com.web.dao.MessageMapper;
import com.web.domain.WebResponse;
import com.web.domain.dataTable.BootstrapTableColumns;
import com.web.entity.Member;
import com.web.entity.Message;
import com.web.manager.jpush.SendJPushMessage;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author FDjavaone
 * @date 2017/9/30
 */
@Controller
public class WebMessageController {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private MemberMapper memberMapper;


    @RequestMapping(value = "sysm/message",
            method = RequestMethod.GET)
    public String message(Model model, HttpServletRequest request) {
        return "Message";
    }


    @Data
    public static class MessageColumns extends BootstrapTableColumns {
        private int isOne;
        private String phone;
        private String title;
        private String content;
    }

    @RequestMapping("/sysm/senderMessage")
    @ResponseBody
    public WebResponse senderMessage(MessageColumns messageColumns) {
        Date date = new Date();
        if (messageColumns.getIsOne() == 1) {
            Member member = memberMapper.selectByMobile(messageColumns.getPhone());
            if (member != null) {
                Message message = new Message();
                message.setTargetName(member.getName());
                message.setTarget(member.getId());
                message.setFromName("系统");
                message.setFrom(0L);
                message.setTitle(messageColumns.getTitle());
                message.setContent(messageColumns.getContent());
                message.setCreateTime(date);
                message.setModifyTime(date);
                messageMapper.insertSelective(message);
                SendJPushMessage.jPushByMessage(message);
                return WebResponse.ok("操作成功");
            } else {
                return WebResponse.fail("消息接收人" + messageColumns.getPhone() + "不存在！");
            }
        } else {
            List<Member> members = memberMapper.selectAllMemeberId();

            for (Member member : members) {
                Message message = new Message();
                message.setTargetName(member.getName());
                message.setTarget(member.getId());
                message.setFromName("系统");
                message.setFrom(0L);
                message.setTitle(messageColumns.getTitle());
                message.setContent(messageColumns.getContent());
                message.setCreateTime(date);
                message.setModifyTime(date);
                messageMapper.insertSelective(message);
                SendJPushMessage.jPushByMessage(message);
            }

            return WebResponse.ok("操作成功");
        }


    }

}
